package example.handler.impl;

import example.constant.PreConditionsCheck;
import example.constant.ResponseEnum;
import example.handler.MoneyHandler;
import example.model.Money;
import example.model.NormalUser;
import example.param.MoneyConsumeParam;
import example.param.MoneyPayParam;
import example.service.MoneyService;
import example.service.NormalUserServie;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by snksnk1091 on 2017/8/23.
 */
@Component
public class MoneyHandlerImpl implements MoneyHandler {

    @Autowired
    private MoneyService moneyService;
    @Autowired
    private NormalUserServie normalUserServie;

    public Object pay(MoneyPayParam param) {
        PreConditionsCheck.checkArgument(moneyService.countMoneyByUserName(param.getUserName()) <= 0, ResponseEnum.CHECK_VOID);
        Money money = moneyService.getMoneyByUserName(param.getUserName());
        DecimalFormat df =new java.text.DecimalFormat("#.00");
        double payNum =  Double.parseDouble(param.getJiaoyiMoney());
        df.format(payNum);
        PreConditionsCheck.checkArgument(payNum < 0, ResponseEnum.PAYMONEY_ERROR);
        money.setId(null);
        money.setJiaoyiMoney(payNum);
        money.setTotalMoney(money.getTotalMoney() + payNum);
        money.setUpdateDate(new Date());
        money.setCreatDate(new Date());
        moneyService.createNewMoney(money);
        normalUserServie.changeMoney(param.getUserName(), payNum);
        return "充值成功，充值金额为：" + payNum;
    }

    public void createNewMoneyByNormalUser(NormalUser normalUser) {
        Money money = new Money();
        money.setUserId(normalUser.getId());
        money.setUserName(normalUser.getUserName());
        double payNum = 0.00;
        if(normalUser.getMoeny()!=0.00){
            payNum = normalUser.getMoeny();
        }
        double jiaoyiMoney = 0.00;
        DecimalFormat df =new java.text.DecimalFormat("#.00");
        df.format(payNum);
        df.format(jiaoyiMoney);
        money.setJiaoyiMoney(jiaoyiMoney);
        money.setTotalMoney(payNum);
        money.setCreatDate(new Date());
        money.setUpdateDate(new Date());
        moneyService.createNewMoney(money);
    }

    public void deleteMoneyByUserName(String userName) {
        moneyService.deleteMoneyByUserName(userName);
    }

    public Object consume(MoneyConsumeParam param) {
        PreConditionsCheck.checkArgument(moneyService.countMoneyByUserName(param.getUserName()) <= 0, ResponseEnum.CHECK_VOID);
        double jiaoyiMoney = Double.parseDouble(param.getJiaoyiMoney());
        DecimalFormat df = new DecimalFormat("#.00");
        df.format(jiaoyiMoney);
        Money money = moneyService.getMoneyByUserName(param.getUserName());
        money.setId(null);
        money.setJiaoyiMoney(-jiaoyiMoney);
        double totalMoney = money.getTotalMoney() - jiaoyiMoney;
        PreConditionsCheck.checkArgument(totalMoney < 0, ResponseEnum.NOTENOUGHMONEY_ERROR);
        money.setTotalMoney(totalMoney);
        money.setUpdateDate(new Date());
        money.setCreatDate(new Date());
        moneyService.createNewMoney(money);
        normalUserServie.changeMoney(param.getUserName(), totalMoney);
        return "交易成功，消费金额：" + jiaoyiMoney;
    }

    public Object getMoneyListByName(String userName) {
        PreConditionsCheck.checkArgument(moneyService.countMoneyByUserName(userName) <= 0, ResponseEnum.CHECK_VOID);
        return moneyService.getMoneyListByUserName(userName);
    }

    public Object getAllMoneyList() {
        List<Money> list = moneyService.getAllMoneyList();
        if(list == null){
            return new ArrayList<Money>();
        }
        return list;
    }
}
